package com.nxk.modules.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nxk.modules.core.entity.SysMenu;
import com.nxk.modules.core.entity.vo.MenuRoleVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ListToJson {

    public static List<SysMenu> getMenuJson(List<SysMenu> list) {
        List<SysMenu> returnList = new ArrayList<>();
        for (SysMenu sysMenu : list) {
            if (sysMenu.getParentId().equals("0")) {
                returnList.add(sysMenu);
                List<SysMenu> children = findChildren(sysMenu.getId(), list);
                sysMenu.setChildren(children);
            }
        }
        return returnList;
    }

    public static List<SysMenu> findChildren(String parentId, List<SysMenu> list) {
        List<SysMenu> levelTypeList = new ArrayList<SysMenu>();
        for (SysMenu sysMenu : list) {
            if (parentId.equals(sysMenu.getParentId())) {
                levelTypeList.add(sysMenu);
                List<SysMenu> children = findChildren(sysMenu.getId(), list);
                sysMenu.setChildren(children);
            }
        }
        return levelTypeList;
    }



    public static List<SysMenu> getMenuJson2(List<SysMenu> list) {
        List<SysMenu> returnList = new ArrayList<>();
        for (SysMenu sysMenu : list) {
            if (sysMenu.getParentId().equals("0")) {
                returnList.add(sysMenu);
                List<SysMenu> children = findChildren2(sysMenu.getId(), list);
                sysMenu.setChildren(children);
            }
        }
        return returnList;
    }

    public static List<SysMenu> findChildren2(String parentId, List<SysMenu> list) {
        List<SysMenu> levelTypeList = new ArrayList<SysMenu>();
        for (SysMenu sysMenu : list) {
            if (parentId.equals(sysMenu.getParentId())) {
                if (SysMenu.MENU.equals(sysMenu.getMenuRole())){
                    sysMenu.setMenuName(sysMenu.getMenuName() + "("+sysMenu.getMenuLink()+")");
                }
                if (SysMenu.ROLE.equals(sysMenu.getMenuRole())){
                    sysMenu.setMenuName(sysMenu.getMenuName() + "("+sysMenu.getMenuIdent()+")");
                }
                levelTypeList.add(sysMenu);
                List<SysMenu> children = findChildren2(sysMenu.getId(), list);
                sysMenu.setChildren(children);
            }
        }
        return levelTypeList;
    }






    public static List<MenuRoleVo> getMenuJson3(List<MenuRoleVo> list) {
        List<MenuRoleVo> returnList = new ArrayList<>();
        for (MenuRoleVo sysMenu : list) {
            if (sysMenu.getParentId().equals("0")) {
                returnList.add(sysMenu);
                List<MenuRoleVo> children = findChildren3(sysMenu.getMenuId(), list);
                sysMenu.setChildren(children);
            }
        }
        return returnList;
    }

    public static List<MenuRoleVo> findChildren3(String parentId, List<MenuRoleVo> list) {
        List<MenuRoleVo> levelTypeList = new ArrayList<MenuRoleVo>();
        for (MenuRoleVo sysMenu : list) {
            if (parentId.equals(sysMenu.getParentId())) {
                levelTypeList.add(sysMenu);
                List<MenuRoleVo> children = findChildren3(sysMenu.getMenuId(), list);
                sysMenu.setChildren(children);
            }
        }
        return levelTypeList;
    }



}
